var app = getApp()

Page({
  data: {
    
    showModal: false, //是否显示中奖页面
    localimgpath:[],
    awardIndex:0,
    msgList_1: [],
    msgList_2:[],
    tmpOfimg: [],
    num:0,
    awardsList: {},
    animationData: {},
    btnDisabled: '',
    mysrc: [],
    showModalStatus: false,
    awardinfo:{},
    imgurl:[],
    btnhiden:false,
    userinfo:{},
    windowWidth: 0,
    windowHeight: 0,
    touch: { x: 0, y: 0, isPressed: false }
  },



  //分享按钮
  onShareAppMessage: function (res){
    wx.showShareMenu({
      withShareTicket: true
    })
    var that=this;
      console.log("进行转发"+JSON.stringify(res))
      var user=that.data.userinfo
    var ShareNickName = user.name;
    var id=user.id
    console.log("获取微信名为：" + ShareNickName)
      return{
        title:"免费抽iPhoneXS限量20台",
        path: '/pages/authorization/index?ShareNickName=' + ShareNickName+"&id="+id,
        success: function (e) {
              console.log("转发完成")
  
        },
        fail: function (res) {
          // 转发失败
          console.log("转发失败:" + JSON.stringify(res));
        }
        
      }
      
  },


  onLoad: function (options){

    var that=this;
    wx.request({
      url: getApp().globalData.url + '/index/app/list',
      success: function (res) {

        if (res.data.code == 200) {

          that.setData({
            mysrc: res.data.data
          })
          that.after(res.data.data.length)

        }

      }, fail: function (res) {
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '网络请求失败',
        })
      }
    })
  },


  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '幸运大转盘',
    })
    var that=this;
    wx.request({
      url: getApp().globalData.url+'/index/app/awardall',
      success: function(res){
          that.setData({
            msgList_1:res.data.data.list1,
            msgList_2: res.data.data.list2
          })
      }
    })   
  },



  onShow: function (options){

    var that = this;
    // 把设备的尺寸赋值给画布，以做到全屏效果

    if (!wx.getStorageSync("scope")) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '您还未授权,请先授权登入',
        success: function (res) {
          console.log("点击确认")
          wx.redirectTo({         
            url: '../authorization/index',
          })
        }
      })
    }


   var that=this
 var length=0;
that.requserinfo();

  },
  getLottery: function () {
    var that = this
    //禁止抽奖按钮
    that.setData({
      btnDisabled: 'disabled'
    })

    var num = that.data.userinfo.lotterynum //抽奖次数
    var awardIndex = 0
    if (num > 0) {
      wx.request({
        data: { token: wx.getStorageSync("token") },
        url: getApp().globalData.url + '/index/app/DrawLottery',
        success: function (res) {
          if (res.data.code == 424) {
            wx.showModal({
              title: '提示',
              content: '抽奖次数不足！',
            })
          }
          that.requserinfo();
          var awardIndex = res.data.data.index;
          console.log("奖品下标" + awardIndex)
          that.setData({
            awardinfo: res.data.data
          })
      
          app.runDegs = app.runDegs || 0
          app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (360 * 8 - awardIndex * (360 / that.data.awardsList.length))

          var animationRun = wx.createAnimation({
            duration: 4000,
            timingFunction: 'ease'
          })
          that.animationRun = animationRun
          animationRun.rotate(app.runDegs).step()
          that.setData({
            animationData: animationRun.export(),
          })

          setTimeout(function () {
            if (res.data.code == 417 || res.data.data.awardname == "谢谢参与") {
              wx.showModal({
                showCancel: false,
                title: '谢谢参与',
                content: '生活不止眼前的苟且，再接再厉！',
                success: function () {


                }
              })
            } else {
              //中奖 
              that.award_on();
            }
            //旋转时间
              that.setData({
                btnDisabled: ''
              })
  
          }, 4500);
        }, fail: function (res) {
          wx.showModal({
            showCancel: false,
            title: '提示',
            content: '网络请求失败',
          })
        }
      })
    } else {

      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '抽奖次数不足！'
      })
      that.setData({
        btnDisabled: ''
      })
    }
  },
 


  requserinfo: function(){
    var that=this;
    wx.request({
      data: {
        token: wx.getStorageSync("token")
      },
      url: getApp().globalData.url + '/app/user/info',
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            userinfo: res.data.data
          })
        }

      }, fail(e) {
        wx.showModal({
          title: '提示',
          content: '网络请求失败',
        })
      }
    })
  },



   after: function (e) {

    var that = this;
    // getAwardsConfig
    app.awardsConfig = {
      chance: true,

    }
    // wx.setStorageSync('awardsConfig', JSON.stringify(awardsConfig))


    // 绘制转盘
    var mysrc = that.data.mysrc
    var awardsConfig = app.awardsConfig.awards,
      len = e,
      rotateDeg = 360 / len / 2 + 90,
      html = [],
      turnNum = 1 / len  // 文字旋转 turn 值
    that.setData({
      btnDisabled: app.awardsConfig.chance ? '' : 'disabled'
    })
    var ctx = wx.createCanvasContext("lotteryCanvas")

    for (var i = 0; i < len; i++) {

      html.push({ turn: i * turnNum + 'turn', lineTurn: i * turnNum + turnNum / 2 + 'turn', src: that.data.mysrc[i].awardimg});
     

    }

     that.setData({
       awardsList: html,
       btnhiden: true

     })




  },


  //中奖弹窗部分函数 
   award_on: function () {
    this.setData({
      showModal: true
    })
  },

 
  gotoList: function () {

    wx.switchTab({
      url: '../list/list'
    })
    this.setData({
      showModal: false
    })
  },
  award_off: function () {

    this.setData({
      showModal: false
    })

  },
 

})
